package org.git.movie.controller;

import org.git.movie.annotations.Verify;
import org.git.movie.form.ListSearchForm;
import org.git.movie.service.FfVodService;
import org.git.movie.util.BaseController;
import org.git.movie.util.BaseResponse;
import org.git.movie.vo.FfVodReadDto;
import org.git.movie.vo.FfVodListDto;
import org.git.movie.vo.PageDto;
import org.git.movie.vo.VodRelationList;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 嘻嘻
 * @since 2020-07-13
 */
@RestController
@RequestMapping("/video")
public class FfVodController  extends BaseController {

    @Resource
    private FfVodService ffVodService;

    @PostMapping("/relation/{videoId}.do")
    public BaseResponse<VodRelationList> relation(@PathVariable(value="videoId") Integer videoId,
                                                     @Verify(param="pageSize",rule="required|min(3)|max(60)") int pageSize){

        PageDto<FfVodListDto> pages =  ffVodService.findRelationVodList(videoId,1,pageSize);
        boolean hasMore = false;
        if(pages.getTotal() >= (pageSize+1)){
            hasMore = true;
        }
        List<FfVodListDto> list = pages.getList();

        VodRelationList rep = new VodRelationList();
        rep.setHasMore(hasMore);
        rep.setList(list);

        return   ok(rep);

    }

    @PostMapping("/relation/more/{videoId}.do")
    public BaseResponse<PageDto<FfVodListDto>> getRelationListMore(@PathVariable(value="videoId") Integer videoId,
                                                     @Verify(param="pageNo",rule="required|min(1)") int pageNo,
                                                      @Verify(param="pageSize",rule="required|min(3)|max(60)") int pageSize){

        PageDto<FfVodListDto> pages =  ffVodService.findRelationVodList(videoId,pageNo,pageSize);


        return   ok(pages);

    }

    @PostMapping("/list/{listid}.do")
    public BaseResponse<PageDto<FfVodListDto>> listVideo(@PathVariable(value="listid") Integer listid,
                                                         @Verify(param="orderdir",rule="enableValueWithEmpty(desc,asc)")String orderDir,
                                                         @Verify(param="orderby",rule="enableValueWithEmpty(vod_hits,vod_addtime,vod_up)")String orderBy,
                                                   @Verify(param="pageNo",rule="required|min(1)") int pageNo,
                                                   @Verify(param="pageSize",rule="required|min(3)|max(60)") int pageSize,
                                                   ListSearchForm listSearchForm
                                                   ){



        listSearchForm.setListid(listid);
        PageDto<FfVodListDto> pages =  ffVodService.findListVodList(listSearchForm,pageNo,pageSize);
        return   ok(pages);

    }
    @PostMapping("/read/{videoId}.do")
    public BaseResponse<FfVodReadDto> read(@PathVariable(value="videoId") Integer videoId ){

        FfVodReadDto dto =  ffVodService.findbyid4dto(videoId);
        return   ok(dto);

    }

    @PostMapping("/play/{videoId}/{playerIndex}/{urlIndex}.do")
    public BaseResponse<FfVodReadDto> play(@PathVariable(value="videoId") Integer videoId ,
                                           @PathVariable(value="playerIndex") Integer playerIndex ,
                                            @PathVariable(value="urlIndex") Integer urlIndex ){

        FfVodReadDto dto =  ffVodService.findbyid4PlayDto(videoId,playerIndex,urlIndex);
        return   ok(dto);

    }
}

